#!/bin/bash
#
###############################################################################
#                                                                             #
#                   Pi-Star Custom Firmware Update Tool                       #
#                                                                             #
#    Version 0.1, Code, Design and Development by Florian Wolters (DF2ET).    #
#                  Based on work by Andy Taylor (MW0MZW)                      #
#                         and Andy Uribe CA6JAU                               #
#                                                                             #
#                 Make it simple to update the Firmware                       #
#                                                                             #
###############################################################################
#
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi
#
# Output some usefull information when no variables are passed
#
if [ -z "$1" ]
then
  echo "This tool is used to flash a custom made firmware to your MMDVM_HS Hat Modem"
  echo ""
  echo "Pre-built nightly firmware configured from the templates can be downloaded from:"
  echo ""
  echo "   https://www.df2et.de/mmdvm_hs/"
  echo ""
  echo "And there is also a firmware builder tool that can be used to build a fully customizable version of the firmware. It is available at:"
  echo ""
  echo "   https://www.df2et.de/mmdvm_hs_builder/"
  echo ""
  echo "Use this script and the generated firmware at your own risk!"
  echo "Usage: $(basename ${0}) <firmware.bin>"
  echo ""
  exit 0
fi
#
# OK we know what the modem is, let get the firmware string from the log...
#
tac $(ls -1rt /var/log/pi-star/MMDVM-*.log | tail -n1 ) | grep "protocol" -m 1 | sed -n 's/description: /&\n/;s/.*\n//p'
read -n 1 -s -r -p "Press any key to write the ${1} firmware to this modem or Ctrl-C to quit..."
echo ""
#
# Download the correct firmware
#
if [ "$1" ]
then
  if [ ! -e "$1" ]; then
     echo "Firmware file $1 not found! Aborting."
  else
    # Download STM32F10X_Lib (only for binary tools)
    if [ ! -d "./STM32F10X_Lib/utils" ]; then
      git clone https://github.com/juribeparada/STM32F10X_Lib
    fi

    # Configure vars depending on OS
    if [ $(uname -s) == "Linux" ]; then
      DEV_USB_SER="/dev/ttyACM0"
      if [ $(uname -m) == "armv7l" ]; then
        echo "Raspberry Pi 3 detected"
        DFU_RST="./STM32F10X_Lib/utils/rpi32/upload-reset"
        DFU_UTIL="./STM32F10X_Lib/utils/rpi32/dfu-util"
        ST_FLASH="./STM32F10X_Lib/utils/rpi32/st-flash"
        STM32FLASH="./STM32F10X_Lib/utils/rpi32/stm32flash"
      elif [ $(uname -m) == "armv6l" ]; then
        echo "Raspberry Pi 2 or Pi Zero W detected"
        DFU_RST="./STM32F10X_Lib/utils/rpi32/upload-reset"
        DFU_UTIL="./STM32F10X_Lib/utils/rpi32/dfu-util"
        ST_FLASH="./STM32F10X_Lib/utils/rpi32/st-flash"
        STM32FLASH="./STM32F10X_Lib/utils/rpi32/stm32flash"
      fi
    fi

    # Stop MMDVMHost process to free serial port
    sudo killall MMDVMHost >/dev/null 2>&1

    # Upload the firmware
    eval sudo $STM32FLASH -v -w "$1" -g 0x0 -R -i 20,-21,21:-20,21 /dev/ttyAMA0
    echo ""

    rm -rf STM32F10X_Lib

    read -n 1 -s -r -p "Flashing ${1} to your modem complete, press any key to reboot your Pi-Star System..."
    echo ""

    shutdown -r now
    exit 0

  fi
fi
exit 0
